package com.example.finalwork.Income;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.finalwork.DAO.IncomeDAO;
import com.example.finalwork.Entity.Income;
import com.example.finalwork.LoginActivity;
import com.example.finalwork.R;
import com.example.finalwork.lmpl.IncomeDAOlmpl;

import java.util.List;

import static org.junit.Assert.assertNotEquals;

public class IncomeDelAcitivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "IncomeDelAcitivity";
    private TextView incomedel_id,incomedel_name,incomedel_date,incomedel_amount,incomedel_remark;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_incomedel);

        incomedel_id = findViewById(R.id.incomedel_id);
        incomedel_name = findViewById(R.id.incomedel_name);
        incomedel_amount = findViewById(R.id.incomedel_amount);
        incomedel_date = findViewById(R.id.incomedel_date);
        incomedel_remark = findViewById(R.id.incomedel_remark);



        findViewById(R.id.income_del_button).setOnClickListener(this);
        findViewById(R.id.income_del_exit).setOnClickListener(this);
        findViewById(R.id.incomedel_query).setOnClickListener(this);

        setTitle("删除收入项目功能");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.incomedel_query:
                IncomeDAO incomeDAO = new IncomeDAOlmpl(this);
                List<Income> incomes = incomeDAO.GetAllIncome();
                for(int i = 0 ; i < incomes.size() ; i++)
                {
                    Income income = incomes.get(i);
                    if(income.getIn_id() == Integer.parseInt(incomedel_id.getText().toString()) && income.getIn_User().equals(LoginActivity.data))
                    {
                        incomedel_name.setText(income.getIn_Name());
                        incomedel_date.setText(income.getIn_Date());
                        incomedel_amount.setText(income.getIn_Amount());
                        incomedel_remark.setText(income.getIn_Remark());
                        Toast.makeText(this,"查询成功！",Toast.LENGTH_SHORT).show();
                        return;
                    }
                }
                Toast.makeText(this,"无查询结果！",Toast.LENGTH_SHORT).show();
                break;
            case R.id.income_del_button:
                IncomeDAO incomeDAO2 = new IncomeDAOlmpl(this);
                int result = incomeDAO2.delIncomeById(Integer.parseInt(incomedel_id.getText().toString()));
                if(result ==0)
                {
                    Toast.makeText(this,"删除失败",Toast.LENGTH_SHORT).show();
                    break;
                }
                Log.d(TAG,"del user result - >" + result);
                assertNotEquals(-1,result);
                Toast.makeText(this,"删除成功，返回上一级",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this,IncomeMainActivity.class));
                break;
            case R.id.income_del_exit:
                Toast.makeText(this,"返回上一级",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this,IncomeMainActivity.class));
                break;
        }
    }
}
